/*
//创建用户相关的小仓库
import { defineStore } from 'pinia'
//引入接口
import { reqLogin, reqUserInfo } from '@/api/user'
//引入数据类型
import type {
  loginForm,
  loginResponseData,
  userResponseData,
} from '@/api/user/type'
import type { UserState } from './types/type'
//引入操作本地存储的工具方法
import { SET_TOKEN, GET_TOKEN, REMOVE_TOKEN } from '@/utils/token'
//引入路由（常量路由）
import { constantRoute } from '@/router/routes'

//创建用户小仓库
let useUserStore = defineStore('User', {
  //小仓库存储数据
  state: (): UserState => {
    return {
      token: GET_TOKEN(), //获取用户唯一标识token
      menuRoutes: constantRoute, //仓库存储生成菜单需要的数组（路由）
      username: '请登录',
      avatar: 'https://gd-hbimg.huaban.com/689a639c005bddbad7bae709038b4fff36b8c9f2cdc8-PTyTst_fw236'
    }
  },
  //异步、逻辑
  actions: {
    //用户登录的方法
    async userLogin(data: loginForm) {
      let result: loginResponseData = await reqLogin(data)
      //成功
      if (result.code == 200) {
        //在pinia仓库储存一下token
        this.token = result.data.token as string
        //本地存储持久化存储一份
        SET_TOKEN(result.data.token as string)
        //保证当前async函数返回一个成功的promise
        return 'ok'
      } else {
        return Promise.reject(new Error(result.data.message))
      }
    },
    //获取用户登录信息的方法
    async userInfo() {
      let result: userResponseData = await reqUserInfo()
      //如果获取用户信息成功，储存用户信息
      if (result.code == 200) {
        this.username = result.data.checkUser.username
        this.avatar = result.data.checkUser.avatar
        return 'ok'
      } else {
        return Promise.reject('获取用户信息失败。')
      }
    },
    //退出登录的方法
    async userLogout() {
      this.token = ''
      this.username = '请登录'
      this.avatar = 'https://gd-hbimg.huaban.com/689a639c005bddbad7bae709038b4fff36b8c9f2cdc8-PTyTst_fw236'
      REMOVE_TOKEN()
    }
  },
  getters: {},
})

//对外暴露获取小仓库的方法
export default useUserStore
*/


//创建用户相关的小仓库
import { defineStore } from 'pinia'
//引入接口
import { reqLogin, reqUserInfo, reqLogout } from '@/api/user'
//引入数据类型
import type {
  loginFormData,
  loginResponseData,
  userInfoResponseData
} from '@/api/user/type'
import type { UserState } from './types/type'
//引入操作本地存储的工具方法
import { SET_TOKEN, GET_TOKEN, REMOVE_TOKEN } from '@/utils/token'
//引入路由（常量路由）
import { constantRoute, asyncRoute, anyRoute } from '@/router/routes'
import router from '@/router'
//引入深拷贝方法
//@ts-ignore
import cloneDeep from 'lodash/cloneDeep'

//用于过滤当前用户需要展示的异步路由
const filterAsyncRoute = (asyncRoute: any, routes: any) => {
  return asyncRoute.filter((item: any) => {
    if (routes.includes(item.name)) {
      if (item.children && item.children.length > 0) {
        item.children = filterAsyncRoute(item.children, routes)
      }
      return true
    }
  })
}
//创建用户小仓库
let useUserStore = defineStore('User', {
  //小仓库存储数据
  state: (): UserState => {
    return {
      token: GET_TOKEN(), //获取用户唯一标识token
      menuRoutes: constantRoute, //仓库存储生成菜单需要的数组（路由）
      username: '请登录',
      avatar: 'https://gd-hbimg.huaban.com/689a639c005bddbad7bae709038b4fff36b8c9f2cdc8-PTyTst_fw236',
      buttons: []  //按钮权限
    }
  },
  //异步、逻辑
  actions: {
    //用户登录的方法
    async userLogin(data: loginFormData) {
      let result: loginResponseData = await reqLogin(data)
      //成功
      if (result.code == 200) {
        //在pinia仓库储存一下token
        this.token = result.data as string
        //本地存储持久化存储一份
        SET_TOKEN(result.data as string)
        //保证当前async函数返回一个成功的promise
        return 'ok'
      } else {
        return Promise.reject(new Error(result.data))
      }
    },
    //获取用户登录信息的方法
    async userInfo() {
      let result: userInfoResponseData = await reqUserInfo()
      //如果获取用户信息成功，储存用户信息
      if (result.code == 200) {
        this.username = result.data.name
        this.avatar = result.data.avatar
        this.buttons = result.data.buttons
        //计算当前用户需要展示的异步路由
        let userAsyncRoute = filterAsyncRoute(cloneDeep(asyncRoute), result.data.routes)
        this.menuRoutes = [...constantRoute, ...userAsyncRoute, ...anyRoute];
        //目前路由器管理的只有常量路由，用户异步路由计算完毕后需动态追加
        [...userAsyncRoute, ...anyRoute].forEach((route: any) => {
          router.addRoute(route)
        })
        return 'ok'
      } else {
        return Promise.reject(new Error(result.message))
      }
    },
    //退出登录的方法
    async userLogout() {
      let result: any = await reqLogout()
      if (result.code == 200) {
        this.token = ''
        this.username = '请登录'
        this.avatar = 'https://gd-hbimg.huaban.com/689a639c005bddbad7bae709038b4fff36b8c9f2cdc8-PTyTst_fw236'
        this.menuRoutes = constantRoute,
        this.buttons = []
        REMOVE_TOKEN()
        return 'ok'
      } else {
        return Promise.reject(new Error(result.message))
      }
    }
  },
  getters: {},
})

//对外暴露获取小仓库的方法
export default useUserStore
